const gulp = require('gulp');

const server = require('gulp-webserver');

const fs = require('fs');

const url = require('url');

const path = require('path');

gulp.task('server',() => {
    gulp.src('./src')
    .pipe(server({
        port:9090,
        // open:true,
        fallback:'promise.html',
        middleware:(req,res,next) => {

            let pathname = url.parse(req.url).pathname;

            if(pathname === '/favicon.ico'){
                return false;
            }

            if(pathname === '/api/getuid'){
                res.end(JSON.stringify({code:1,uid:'1'}))
            }else if(pathname === '/api/list'){
                //uid  get
                let uid = url.parse(req.url,true).query.uid;

                // if(uid){
                    res.end(JSON.stringify({code:1,data:'list'}))
                // }     
            }else if(pathname === '/api/banner'){
                res.end(JSON.stringify({code:1,data:'banner'}))
            }else{
                pathname = pathname === '/' ? 'promise.html' : pathname;
                res.end(fs.readFileSync(path.join(__dirname,'src',pathname)));
            }
        }
    }))
})